﻿//TODO: document everthing in this file.

/*
 *  Written By: Ahmad Soboh and Ahmad Assadi.
 *  All Rights Reserved - 2013.
*/

var isLoaded = false;

function loadFaculties() {
    if (isLoaded == true) {
        if (document.getElementById("flink").textContent == '<<') {
            document.getElementById("flink").textContent = '>>';
            document.getElementById("flist").style.visibility = 'hidden';
        }
        else if (document.getElementById("flink").textContent == '>>') {
            document.getElementById("flink").textContent = '<<';
            document.getElementById("flist").style.visibility = 'visible';
        }
        return;
    }
    isLoaded = true;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var j = JSON.parse(xmlhttp.responseText);
            //document.getElementById("flink").style.visibility = 'hidden';
            document.getElementById("flink").textContent = '<<';
            var fdiv = document.getElementById("flist");
            for (var i = 0; i < j.length; i++) {
                var opt = j.facultiesJsonList[i].name;
                var newCheckBox = document.createElement('input');
                newCheckBox.type = 'checkbox';
                newCheckBox.id = "faculty" + i.toString();
                //use the same name for all checkboxes, needed to let the action catch them:
                newCheckBox.name = "faculty";
                newCheckBox.value = j.facultiesJsonList[i].name;
                fdiv.appendChild(newCheckBox);
                fdiv.innerHTML += " " + opt + " ";
                if ((i + 1) % 3 == 0) {
                    fdiv.innerHTML += "<br/>";
                }
            }
        }
        document.getElementById("wait").style.display = "none";
    }
    xmlhttp.open("GET", "/Courses/GetFaculty", true);
    document.getElementById("wait").style.display = "block";
    xmlhttp.send();
}

function selectCourse(number, name, id, points) {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if (document.getElementById(id).value == "Select") {
                document.getElementById(id).value = "Unselect";
            }
            else if (document.getElementById(id).value == "Unselect") {
                document.getElementById(id).value = "Select";
            }
        }
    }
    if (document.getElementById(id).value == "Select") {
        xmlhttp.open("GET", "/Courses/Select/?number=" + number + "&name=" + name + "&points=" + points, true);
    }
    else if (document.getElementById(id).value == "Unselect") {
        xmlhttp.open("GET", "/Courses/Unselect/?number=" + number, true);
    }
    xmlhttp.send();
}